"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BaseLoader = exports.LoaderErrors = exports.LoaderStatus = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _exception = require("./utils/exception.js");

/*
 * Copyright (C) 2016 Bilibili. All Rights Reserved.
 *
 * @author zheng qian <xqq@xqq.im>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var LoaderStatus = {
  kIdle: 0,
  kConnecting: 1,
  kBuffering: 2,
  kError: 3,
  kComplete: 4
};
exports.LoaderStatus = LoaderStatus;
var LoaderErrors = {
  OK: 'OK',
  EXCEPTION: 'Exception',
  HTTP_STATUS_CODE_INVALID: 'HttpStatusCodeInvalid',
  CONNECTING_TIMEOUT: 'ConnectingTimeout',
  EARLY_EOF: 'EarlyEof',
  UNRECOVERABLE_EARLY_EOF: 'UnrecoverableEarlyEof'
};
/* Loader has callbacks which have following prototypes:
 *     function onContentLengthKnown(contentLength: number): void
 *     function onURLRedirect(url: string): void
 *     function onDataArrival(chunk: ArrayBuffer, byteStart: number, receivedLength: number): void
 *     function onError(errorType: number, errorInfo: {code: number, msg: string}): void
 *     function onComplete(rangeFrom: number, rangeTo: number): void
 */

exports.LoaderErrors = LoaderErrors;

var BaseLoader = /*#__PURE__*/function () {
  function BaseLoader(typeName) {
    (0, _classCallCheck2.default)(this, BaseLoader);
    this._type = typeName || 'undefined';
    this._status = LoaderStatus.kIdle;
    this._needStash = false; // callbacks

    this._onContentLengthKnown = null;
    this._onURLRedirect = null;
    this._onDataArrival = null;
    this._onError = null;
    this._onComplete = null;
  }

  (0, _createClass2.default)(BaseLoader, [{
    key: "destroy",
    value: function destroy() {
      this._status = LoaderStatus.kIdle;
      this._onContentLengthKnown = null;
      this._onURLRedirect = null;
      this._onDataArrival = null;
      this._onError = null;
      this._onComplete = null;
    }
  }, {
    key: "isWorking",
    value: function isWorking() {
      return this._status === LoaderStatus.kConnecting || this._status === LoaderStatus.kBuffering;
    }
  }, {
    key: "open",
    // pure virtual
    value: function open(dataSource, range) {
      throw new _exception.NotImplementedException('Unimplemented abstract function!');
    }
  }, {
    key: "abort",
    value: function abort() {
      throw new _exception.NotImplementedException('Unimplemented abstract function!');
    }
  }, {
    key: "type",
    get: function get() {
      return this._type;
    }
  }, {
    key: "status",
    get: function get() {
      return this._status;
    }
  }, {
    key: "needStashBuffer",
    get: function get() {
      return this._needStash;
    }
  }, {
    key: "onContentLengthKnown",
    get: function get() {
      return this._onContentLengthKnown;
    },
    set: function set(callback) {
      this._onContentLengthKnown = callback;
    }
  }, {
    key: "onURLRedirect",
    get: function get() {
      return this._onURLRedirect;
    },
    set: function set(callback) {
      this._onURLRedirect = callback;
    }
  }, {
    key: "onDataArrival",
    get: function get() {
      return this._onDataArrival;
    },
    set: function set(callback) {
      this._onDataArrival = callback;
    }
  }, {
    key: "onError",
    get: function get() {
      return this._onError;
    },
    set: function set(callback) {
      this._onError = callback;
    }
  }, {
    key: "onComplete",
    get: function get() {
      return this._onComplete;
    },
    set: function set(callback) {
      this._onComplete = callback;
    }
  }]);
  return BaseLoader;
}();

exports.BaseLoader = BaseLoader;